package com.bauer.java.spring.fengtai;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @description:
 * @author: weirui
 * @create: 2019-11-26 19:58
 **/
public abstract class AbsDebitFileParser<T> {




    public void parse(String localPath,String filename) throws IOException {

        //copy文件到本地
        copyFileToLocal(localPath,filename);

        //解析文件数据
        analysisData(localPath);

    }


    public void copyFileToLocal(String localPath,String fileName){

    }

    abstract void handlerBodyData(String[] body);

    abstract void handlerHeadData(String[] head);

    abstract void doAfterAnalysisFile();

    abstract FileParserConfig getConfig();

    private void analysisData(String localPath) throws IOException {

        File file = new File(localPath);
        if (!file.exists()) {
            return;
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        String lineData = null;
        FileParserConfig fileParserConfig = getConfig();
        boolean firstLine = true;
        while ((lineData = reader.readLine()) != null) {
            if (firstLine && fileParserConfig.isHasHead()) {

                handlerHeadData(lineData.split(fileParserConfig.getFiledSeparator()));
                firstLine = false;
                continue;
            }

            handlerBodyData(lineData.split(fileParserConfig.getFiledSeparator()));
        }

        doAfterAnalysisFile();

        //删除临时文件
        file.deleteOnExit();
    }


}
